#include "stdafx.h"
#include "ckFieldDef.h"
#include "ckValue.h"

namespace ck
{
	namespace data
	{
		FieldDef::FieldDef()
		{
			m_type = ckDataTypeUnknown;
			m_pDefaultValue = NULL;
			m_precision = 0;
			m_length = 0;
			m_nullable = true;
		}


		FieldDef::~FieldDef()
		{
			if (m_pDefaultValue != NULL)
			{
				m_pDefaultValue->Release();
				m_pDefaultValue = NULL;
			}
		}

		FieldDef* FieldDef::Create(ckDataType type, z_int32 length/*=0*/, z_uint32 decimal/*=0*/, Value* pDefValue/*=NULL*/)
		{
			FieldDef* pDef = new FieldDef();
			pDef->m_type = type;
			pDef->m_length = length;
			pDef->m_precision = decimal;
			pDef->m_pDefaultValue = pDefValue;

			return pDef;
		}

		ckDataType FieldDef::GetType()
		{
			return m_type;
		}

		z_int32	FieldDef::GetLength()
		{
			return m_length;
		}

		z_uint32 FieldDef::GetDecimals()
		{
			return m_precision;
		}

		Value* FieldDef::GetDefaultValue()
		{
			return m_pDefaultValue;
		}

		void FieldDef::Set(ckDataType type, bool nullable)
		{
			m_type = type;
			m_nullable = nullable;
		}

		void FieldDef::SetPrecision(z_int32 precision)
		{
			m_precision = precision;
		}

		void FieldDef::SetDefaultValue(Value* pValue)
		{
			if (m_pDefaultValue)
			{
				m_pDefaultValue->Release();
				m_pDefaultValue = NULL;
			}
			m_pDefaultValue = pValue;
			
		}

		bool FieldDef::IsNullable()
		{
			return m_nullable;
		}

	}
}

